const babel = require("gulp-babel")
const gulp = require("gulp")

const baseConfig = {
    babelrc: false,
    presets: [
        [
            "@babel/preset-env",
            {
                targets: {
                    "node": "current"
                }
            }
        ],
        "@babel/preset-typescript",
        "@babel/preset-react"
    ],
    plugins: [
        ["@babel/plugin-transform-runtime", {
            helpers: false,
            regenerator: true
        }],
        ["@babel/plugin-proposal-class-properties", {
            loose: true
        }],
        ["@babel/plugin-proposal-decorators", {
            legacy: true
        }],
        "@babel/plugin-proposal-json-strings",
        "@babel/plugin-proposal-function-sent",
        "@babel/plugin-proposal-export-namespace-from",
        "@babel/plugin-proposal-numeric-separator",
        "@babel/plugin-proposal-throw-expressions",
        "@babel/plugin-syntax-dynamic-import",
        "@babel/plugin-syntax-import-meta",
        "@babel/plugin-transform-modules-commonjs"
    ]
};

const transpile = (src, dest) => {
    return gulp.src(src).pipe(babel(baseConfig)).pipe(gulp.dest(dest || `./dist/src/`));
}
const transpileAll = function () {
    gulp.src(`./src/**/*.json`).pipe(gulp.dest(`./dist/src/`));
    return transpile(`./src/**/*.js`);
}

gulp.task("transpile", transpileAll);

gulp.task("transpile-watch", () => {
    gulp.watch(['./src/**/*.js', './server.js', './renderPage.js'], {
        ignoreInitial: false,
        queue: true
    }, gulp.series("transpile"));

});